<?php

namespace app\interfaces\controller;


use think\Db;
use app\interfaces\model\RushHour as RushHourModel;
use app\interfaces\model\QuotHour as QuotHourModel;
use think\Request;
use think\Loader;
class Limited

{

    public function alipay(){
        $timestamp = date('Y-m-d H:i:s',time());
        $sign = 'AxbeZ0lhy8FJ42sRhyB+VsnDE+H2AF7mrxg8B3GZ3UiMziZClCELgJAOMwyLfjf1dhCBCqhLp9T3/d5VZb752TBy/ELPTdooLdTR4Bo38hLoaApci0zFuqdkJBhGy3l/q/f3USIB+bFHwAfVfZ3Pj2fHXN+5mGgJaFYMuJuOXK78BBRxeIUHYKyalrtZEecNO5qZjnJPJcrfuQyP31WJnfoDNSgE1bHs0KIntYSlddNnJZF45f92nKY7uDXTi8057mlcnQSPgFcJvqitXu2dgIJm0bZCuPZx6aRTSmEGTIeKYuMdzSNUji2ZQgzl6VQYEqbTjmIPHGKO+dxSDWnDSQ==';
        return $this->curl_request('https://openapi.alipay.com/gateway.do?timestamp='.$timestamp.'&method=alipay.fund.trans.toaccount.transfer&app_id=2018053160276379&sign_type=RSA2&sign='.$sign.'&version=1.0&biz_content=
  {
"out_biz_no":"201808031035066963658119",
"payee_type":"ALIPAY_LOGONID",
"payee_account":"abc@sina.com",
"amount":"0.01",
"payer_show_name":"上海交通卡退款",
"payee_real_name":"张三",
"remark":"转账备注"
  }');

    }

    public function news($order_number,$openid,$pay_account,$pay_name){
//        halt("{" .
//            "\"out_biz_no\":\"$order_number\"," .
//            "\"payee_type\":\"ALIPAY_LOGONID\"," .
//            "\"payee_account\":\"$openid\"," .
//            "\"amount\":\"$pay_account\"," .
//            "\"payer_show_name\":\"善食元商城支付宝提现\"," .
//            "\"payee_real_name\":\"\"," .
//            "\"remark\":\"提现\"" .
//            "}");
        Loader::import('alipay.AopSdk');
        Loader::import('alipay.aop.request.AlipayFundTransToaccountTransferRequest');
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2018053160276379';
        $aop->rsaP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AJzzXEETRyqrDr7mSTyJOXxhECgYEA1VCIYuRDk88S4g+9V0nexZM5AmXf6flHAD3GH6VBLNYf9axIWhEnMESlmsLsid/mFuO2xJqxfwBYQ8cH2hKpfNqpypKj6D+qKS5maAg295sSkLr0pZvX47XoQEYjF7dacToy5NURBpPp+7FihmyU8uRK9HXJJMikYTsJmOju55UCgYAdlEwyXXgpiI6fqCpiEf6t9bfz8SN84pMB1vLuCvlbYj54d2wQtL5mWJgKW2E7IpCCZ/zyRgtJKE9Tmlgz8ENGop0T+BjyTKNMpYDpw5zShCadyzWtKhiHDNTZU82C4rO5YGY4cC259knnciq6G+v6/HeCdev2fOQqkFdRuz2tUQKBgFmW1jHBTIMZ4QT0/mC5yww2dNrexYOuPPbPsk5BP3Pri5JKSpV5IIyIJf1H6l/6t+4bquv2DpkdFI+xPIm96hSiQR4TUxwUIUKeJXuLorzTWUNx51NzhazfVcolucWbo2PbRNO/mrrJoobI/PmYFcRHX+qRXvo1pyYo6cNnXgUf';
        $aop->alipayrsaPublicKey='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArkvOKDcsDkvMTpYuuA3QnY6pn3g8Uej9Lp4JRw64EjVi87nTE2YZCHRpQBSkC99yCRwqx7FO9GcKP2tT4GrdK0mWOiV5z1y2s6fj4DRreCsGrwH1640jLjEiJL4+byxKeqWE0m4K9Mpa3Jx/IGxDhAhAMILnuWPrRBjhiORygalHvPHO2q1pKAXrewrMRS7zxlbjUp3KHIaL8QVBQa0GbIc/chBGauJ+sqorRVO+2l0zuwLLuNXhaVaAWXoomPFQRS2SFQf8ubE7Io4nWRNXqVQR7k52fG6dBH7kMnAzsPRn7g88t3FXHt86gAG/SXfzgKN/1ubVz14iyfVWL8AbbwIDAQAB';
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new \AlipayFundTransToaccountTransferRequest();
        $request->setBizContent("{" .
            "\"out_biz_no\":\"$order_number\"," .
            "\"payee_type\":\"ALIPAY_LOGONID\"," .
            "\"payee_account\":\"$openid\"," .
            "\"amount\":\"$pay_account\"," .
            "\"payer_show_name\":\"善食元商城支付宝提现\"," .
            "\"payee_real_name\":\"\"," .
            "\"remark\":\"提现\"" .
            "}");
        $result = $aop->execute ($request);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            return true;
        } else {
            $resultMsg = $result->$responseNode->sub_msg;
            return $resultMsg;
        }
    }
    public function curl_request($url,$post='',$cookie='', $returnCookie=0){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
        if($post) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
        }
        if($cookie) {
            curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        }
        curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)) {
            return curl_error($curl);
        }
        curl_close($curl);
        if($returnCookie){
            list($header, $body) = explode("\r\n\r\n", $data, 2);
            preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
            $info['cookie']  = substr($matches[1][0], 1);
            $info['content'] = $body;
            return $info;
        }else{
            return $data;
        }
    }

    /*

     * 用户端 - 限时抢购列表

     */

    public function limited_list()

    {


        $result = Db::view('limited', 'parenTid,goods_id,price,number,purchase_number,status,create_time')
            ->view('limited_type', 'end_time', 'limited_type.id = limited.parenTid')
            ->view('shop_goods', 'id,title,thumb_img,price as goods_price,describe,rebate_percentage', 'shop_goods.id = limited.goods_id')
            ->where(['status' => '1', 'end_time' => ['gt', time()]])
            ->order(['create_time' => 'desc'])
            ->paginate(10)
            ->toArray();


        // 判断是否存在

        if (!empty($result['data'])) {

            $find['current_time'] = time();

            $find['end_time'] = $result['data'][0]['end_time'];

            return ['code' => 200, 'message' => '成功', 'data' => $result, 'time' => $find];

        } else {

            $find['current_time'] = 0;

            $find['end_time'] = 0;

            return ['code' => 200, 'message' => '本场活动已结束', 'data' => $result, 'time' => $find];

        }


    }


    public function ceshi()
    {
        // 获取数据
        $type_id = Request::instance()->param('type_id');

        // 实例化
        $quotaTime = new QuotHourModel;

        // 今天的开始时间撮
        $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));

        // 当前时间的活动
        $hour = (int)date('H');
        $ini['end_time'] = ['gt', $hour];
        $now_spot = $quotaTime->where($ini)->field('id,name,start_time,end_time')->value('id');

        // 距离活动结束 开始时间
        if ($now_spot == $type_id) {
            $end_time = $quotaTime->where('id', $type_id)->value('end_time');
            $end_time = $beginToday + ($end_time * 3600);
            $limit_time = $end_time - time();

        } else {
            $end_time = $quotaTime->where('id', $type_id)->value('start_time');
            $end_time = $beginToday + ($end_time * 3600);
            $limit_time = time() - $end_time;

        }

        if ($limit_time<0){
            $limit_time =-$limit_time;
        }

        $result = Db::view('rush_hour', 'rush_id,goods_id,price,number,purchase_number,create_time')
            ->view('shop_goods', 'id,title,thumb_img,price as goods_price,describe,rebate_percentage', 'shop_goods.id = rush_hour.goods_id')
            ->where('rush_id',$type_id)
            ->order(['create_time' => 'desc'])
            ->paginate(10)->toArray();

        return ['code' => 200, 'message' => 'OK', 'limit_time' => $limit_time, 'data' => $result];
    }


    public function ceshi_time()
    {
        $quotaTime = new QuotHourModel;
        $hour = date('H');
        $ini['end_time'] = ['gt', $hour];
        $type = $quotaTime->where($ini)->field('id,name,start_time,end_time')->limit(5)->select()->toArray();
        $type_total = count($type);
        if ($type_total<5){
            $difference = 5-$type_total;
            $difference_type = $quotaTime->field('id,name,start_time,end_time')->limit($difference)->select()->toArray();
            $type = array_merge($type,$difference_type);
        }


        return ['code' => 200, 'message' => 'OK', 'data' => $type];
    }


}

